using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TowerMan : Npc
{
    [Header("魔化")]
    public GameObject magicObj;
    [Header("未入魔数据")]
    public SayData data1;
    [Header("死亡魔数据")]
    public SayData data2;
    [Header("触发魔战斗数据")]
    public SayData data4;
    [Header("coll")]
    public GameObject collObj;
    [Header("塔灵")]
    public GameObject towerSoul;
    [Header("塔灵数据")]
    public SayData data3;
    [Header("名称")]
    public Text myNameText;
    [Header("攻击的物体")]
    public GameObject attack1;
    public GameObject attack2;
    [Header("子弹父物体")]
    public Transform attackParent;

    //开启战斗
    public bool canBattle = false;
    //释放技能时间
    public float attackTimer = 1f;
    public float attackMaxTimer = 30;
    public float attackMinTimer = 20;

    //关闭数据列表
    public List<GameObject> closeList = new List<GameObject>();
    //打开列表
    public List<GameObject> openList = new List<GameObject>();

    //所有点位
    public List<Transform> allPos = new List<Transform>();

    private void Awake()
    {
        GameManager.Instance.towerMan=gameObject;

        //杀死公主
        if(GameManager.Instance.isKillPro)
        {
            attackMinTimer = 10;
            attackMaxTimer = 20;
        }
    }
    //打开魔化
    public void OpenMagic()
    {
        if(GameManager.Instance.haveSword)
        {
            CloseSelf();
            magicObj.SetActive(true);
            myNameText.text=data2.myName;
        }
    }
    //关闭自身
    public void CloseSelf()
    {
        GetComponent<SpriteRenderer>().enabled=false;
    }
    //释放特效
    public void PlayEffect()
    {
        StartCoroutine(Die());
    }
    //死亡
    public IEnumerator Die()
    {
        //无法操作
        GameManager.Instance.canOprate=false;
        //播放滑刀特效
        GameManager.Instance.player.GetComponent<PlayerHurtObj>().hurtObj.GetComponent<Animator>().SetTrigger("Attack");
        GameManager.Instance.player.GetComponent<PlayerHurtObj>().hurtObj.transform.position=transform.position;

        yield return new WaitForSeconds(0.5f);

        //播放死亡特效
        magicObj.GetComponent<Animator>().SetTrigger("Die");
        GameManager.Instance.isWin=true;

        yield return new WaitForSeconds(0.6f);
        //打开塔灵
        OpenTowerSoul();
        //可以操作
        GameManager.Instance.canOprate=true;
    }
    //打开塔灵
    public void OpenTowerSoul()
    {
        if(GameManager.Instance.isWin)
        {
            CloseSelf();
            magicObj.SetActive(false);
            towerSoul.SetActive(true);
            myNameText.text=data3.myName;
        }
    }

    private void Start()
    {
        collObj = transform.Find("CheckPosOne").gameObject;

        //隐藏
        collObj.GetComponent<Collider2D>().enabled = false;

        myNameText.text=data1.myName;

        OpenMagic();
        OpenTowerSoul();
    }
    private void Update()
    {
        //计算时间
        CalculateTimer();
    }
    private void OnMouseDown()
    {
        if (GameManager.Instance.canOprate==false) return;

        SceneItemManager.instance.clickEnemyRight = true;
        collObj.GetComponent<Collider2D>().enabled = true;
        SceneItemManager.instance.SetClickEnemy(gameObject);
    }
    //打开UI
    public void OpenLove()
    {
        if(magicObj.activeSelf)
        {
            sayData=data4;
        }
        else
        {
            if(towerSoul.activeSelf)
            {
                sayData=data3;
            }
            else
            {
                sayData=data1;
            }
        }
        sayData.type=SayType.Npc;
        PanelManager.Open<SayPanel>(sayData);

        //关闭碰撞enable
        collObj.GetComponent<Collider2D>().enabled = false;
    }
    //开启战斗
    public void StartBattle()
    {
        canBattle=true;
        OpenAll();
        CloseAll();
        //保存所有数据
        SaveManager.instance.SaveAllData();
    }
    //结束战斗
    public void OverBattle()
    {
        canBattle=false;

        //打开剧情杀
        sayData=data2;
        sayData.type=SayType.Npc;
        PanelManager.Open<SayPanel>(sayData);
        //清空所有攻击
        DesAllAttack();
    }
    //战斗
    public void Battle()
    {
        if(canBattle)
        {
            CalculateTimer();
        }
    }
    //计算时间
    public void CalculateTimer()
    {
        if (canBattle==false) return;
        if(attackTimer<=0)
        {
            RandomTimer();
            //释放攻击
            if(Random.Range(0,100)<50)
            {
                Attack1();
            }
            else
            {
                Attack2();
            }
        }
        else
        {
            attackTimer-=Time.deltaTime;
        }
    }
    //释放技能1
    public void Attack1()
    {
        Player player = GameManager.Instance.player;
        List<Vector3> posList = new List<Vector3>();
        foreach(var i in allPos)
        {
            posList.Add(i.position);
        }
        //选择一个和玩家最近的点位
        int minDisIndex = 0;
        float minDis = Vector2.Distance(posList[0], player.transform.position);
        for(int i=1;i<posList.Count;i++)
        {
            float currDis = Vector2.Distance(posList[i], player.transform.position);
            if (Vector2.Distance(posList[i], player.transform.position)<minDis)
            {
                minDisIndex=i;
                minDis=currDis;
            }
        }
        GameObject newObj = Instantiate(attack1);
        newObj.transform.position=posList[minDisIndex];
        posList.RemoveAt(minDisIndex);
        newObj.transform.SetParent(attackParent);
        //随机选择另外两个点位
        //注意不能相同
        for(int i=0;i<4;i++)
        {
            newObj=Instantiate(attack1);
            int currIndex = Random.Range(0, posList.Count);
            newObj.transform.position=posList[currIndex];
            posList.RemoveAt(currIndex);
            newObj.transform.SetParent(attackParent);
        }
    }
    //释放技能2
    public void Attack2()
    {
        //朝左边散发
        //暂定7发,第一个180，每个相隔20度
        for(int i=0;i<7;i++)
        {
            GameObject newObj = Instantiate(attack2);
            newObj.transform.position=transform.position;
            newObj.transform.SetParent(attackParent);
            //角度设置 
            if (i<=3)
            {
                attack2.transform.eulerAngles=new Vector3(0, 0, 180+i*20);
            }
            else
            {
                attack2.transform.eulerAngles=new Vector3(0, 0, 180-(i-3)*20);
            }
        }
    }
    //随机选择时间
    public void RandomTimer()
    {
        attackTimer=Random.Range(attackMinTimer, attackMaxTimer)/10;
    }
    //关闭所有道具
    public void CloseAll()
    {
        foreach(var i in closeList)
        {
            if(i!=null)
            {
                i.SetActive(false);
            }
        }
    }
    //打开道具
    public void OpenAll()
    {
        bool isFlag = true;
        foreach(var i in openList)
        {
            if(isFlag)
            {
                isFlag=false;
                i.SetActive(true);
                SceneItemManager.instance.SetStop(i.transform);
            }
            else
            {
                i.SetActive(true);
            }
        }
    }
    //销毁所有子弹
    public void DesAllAttack()
    {
        for(int i=0;i<attackParent.childCount;i++)
        {
            GameObject childObj = attackParent.GetChild(i).gameObject;
            if (childObj!=null)
            {
                Destroy(childObj);
            }
        }
    }
}